###############################################
# ORDER OF THE SACRED MAGDALENE
# MAGDALENE SUCCESSION EVENTS
#
# ggsm.600 = calculate Grace cost and confirm reform
# ggsm.601 = annual hidden maintenance
#
# Uses long_character_event throughout for compatibility
# with Even Bigger Text Boxes.
###############################################

namespace = ggsm


###############################################
# 600 - ESTABLISH MAGDALENE SUCCESSION
###############################################

long_character_event = {
    id = ggsm.600
    desc = ggsm.600.desc
    picture = GFX_evt_ggsm_scripture_study
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    trigger = {
        society_member_of = ggsm_order_sacred_magdalene
		society_rank >= 4
        is_landed = yes

        OR = {
            is_feudal = yes
            is_tribal = yes
        }

        NOT = {
            has_character_flag = ggsm_magdalene_succession_enacted
        }
    }

    immediate = {
        # Store 50% of current Grace as a negative local variable.
        export_to_variable = {
            which = local_ggsm_magdalene_succession_grace_cost
            value = society_currency
        }

        multiply_variable = {
            which = local_ggsm_magdalene_succession_grace_cost
            value = -0.50
        }
    }

    option = {
        name = ggsm.600.a

        custom_tooltip = {
            text = ggsm_magdalene_succession_cost_tt
        }

        custom_tooltip = {
            text = ggsm_magdalene_succession_effect_tt
        }

        # Pay exactly 50% of current Grace.
        # Explicit society scope prevents ambiguity.
        change_society_currency = {
            society = ggsm_order_sacred_magdalene
            value = local_ggsm_magdalene_succession_grace_cost
            scaled_by_influence = no
        }

        set_character_flag = ggsm_magdalene_succession_enacted

        # Apply Enatic Primogeniture to every county-or-higher title
        # personally held by the ruler. Baronies are deliberately left
        # alone because city and temple baronies use special succession.
        any_demesne_title = {
            limit = {
                OR = {
                    tier = COUNT
                    tier = DUKE
                    tier = KING
                    tier = EMPEROR
                }
            }

            succession = primogeniture

            add_law = {
                law = enatic_succession
                cooldown = no
                opinion_effect = yes
            }
        }

        # Force all feudal and tribal landed rulers in the realm to use
        # the same Enatic Primogeniture succession on their county-or-higher
        # titles. Nomads and special republic/theocracy mechanics are untouched.
        any_realm_lord = {
            limit = {
                is_landed = yes

                OR = {
                    is_feudal = yes
                    is_tribal = yes
                }
            }

            any_demesne_title = {
                limit = {
                    OR = {
                        tier = COUNT
                        tier = DUKE
                        tier = KING
                        tier = EMPEROR
                    }
                }

                succession = primogeniture

                add_law = {
                    law = enatic_succession
                    cooldown = no
                    opinion_effect = yes
                }
            }
        }

        # Recheck the realm yearly while this ruler remains eligible.
        hidden_tooltip = {
            long_character_event = {
                id = ggsm.601
                days = 365
            }
        }
    }

    option = {
        name = ggsm.600.b

        # Local event variables expire with the event chain.
    }
}


###############################################
# 601 - ANNUAL SUCCESSION MAINTENANCE
###############################################

long_character_event = {
    id = ggsm.601
    hide_window = yes
    is_triggered_only = yes

    trigger = {
        has_character_flag = ggsm_magdalene_succession_enacted
        society_member_of = ggsm_order_sacred_magdalene
        is_landed = yes

        OR = {
            is_feudal = yes
            is_tribal = yes
        }
    }

    immediate = {
        # Keep newly acquired personal titles synchronized.
        any_demesne_title = {
            limit = {
                OR = {
                    tier = COUNT
                    tier = DUKE
                    tier = KING
                    tier = EMPEROR
                }
            }

            succession = primogeniture

            add_law = {
                law = enatic_succession
                cooldown = no
                opinion_effect = yes
            }
        }

        # Keep existing and newly acquired feudal/tribal vassals synchronized.
        any_realm_lord = {
            limit = {
                is_landed = yes

                OR = {
                    is_feudal = yes
                    is_tribal = yes
                }
            }

            any_demesne_title = {
                limit = {
                    OR = {
                        tier = COUNT
                        tier = DUKE
                        tier = KING
                        tier = EMPEROR
                    }
                }

                succession = primogeniture

                add_law = {
                    law = enatic_succession
                    cooldown = no
                    opinion_effect = yes
                }
            }
        }

        hidden_tooltip = {
            long_character_event = {
                id = ggsm.601
                days = 365
            }
        }
    }
}
